<?php
	class AloneAction extends CommonAction{
		public function index(){
			$model = M('Category');
			$map['pid'] = 0;
			$map['type'] = 'alone';
			//$map['url'] = null;
			//$map['status'] = 1;
			$list = $model->where( $map )->order('sort')->select();
			$html = '';
			foreach($list as $key=>$val){
				$html .= '<tr>' .
					'<td align="left"><a href="'.U('edit',array('id'=>$val['id'],'tag'=>$_GET['tag'])).'">' . $val['name'] . '</a></td>' . 
					'<td align="center">' . getStatus($val['status']) . '</td>' .
					'<td align="center">'.
					'<a href="'.U('edit',array('id'=>$val['id'],'tag'=>$_GET['tag'])).'">编辑</a>'.
					'</td>' . 
				'</tr>';
				$html .= $this->getSonlist($model, $val['id'], '├──');
			}
			Cookie::set ( '_currentUrl_', __SELF__ );
			$this->assign("list",$html);
			$this->display();
		}
		
		public function edit(){
			$model = D('Alone');
			$data = $model->where("cid='".$_GET['id']."'")->find();
			if (empty($data)){
				$new['cid'] = $_GET['id'];
				$model->add($new);
			}
			$this->assign("data",$data);
			$this->display();
		}
		
		public function update(){
			$model = D('Alone');
			$_POST['title'] = trim($_POST['title']);
			$_POST['content'] = stripslashes(trim($_POST['content']));
			$_POST['update_time'] = time();
			$_POST['author'] = $_SESSION[C('USER_AUTH_KEY')];
			if ($model->where("cid='".$_POST['cid']."'")->save($_POST)){
				$this->assign("jumpUrl",Cookie::get ( '_currentUrl_' ));
				$this->success('修改完成');
			}
		}
		
		private function getSonlist($model,$pid,$step){
			$data = $model->where("pid='".$pid."'")->select();
			$html = '';
			if (!empty($data)){
				foreach ($data as $key=>$val){
					$html .= '<tr>' .
						'<td align="left">'. $step .' <a href="'.U('edit',array('id'=>$val['id'],'tag'=>$_GET['tag'])).'">' . $val['name'] . '</a></td>' . 
						'<td align="center">' . getStatus($val['status']) . '</td>' .
						'<td align="center">'.
						'<a href="'.U('edit',array('id'=>$val['id'],'tag'=>$_GET['tag'])).'">编辑</a>'.
						'</td>' . 
					'</tr>';
					$html .= $this->getSonlist($model, $val['id'], '<span style="color:#ccc;">├──</span> '.$step);
				}
			}
			return $html;
		}
	}
